package com.dtw.ditouwang.ui.more;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.beans.FrequenctBean;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FrequenctActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    public static final String KeyID = "KeyID";
    List<FrequenctBean.DataBean> dataBeanList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frequenct);
        enableBack();
        setTitle("举报");
        final ListView listView = findViewById(R.id.listview);

        webApi.getFrequency().enqueue(new Callback<FrequenctBean>() {
            @Override
            public void onResponse(Call<FrequenctBean> call, Response<FrequenctBean> response) {
                if (response.body().isResult()&&!isFinishing()) {
                    dataBeanList = response.body().getData();
                    String[] frequency = new String[response.body().getData().size()];
                    for (int i = 0; i < response.body().getData().size(); i++) {
                        frequency[i] = response.body().getData().get(i).getName();
                    }
                    listView.setAdapter(new ArrayAdapter(FrequenctActivity.this, android.R.layout.simple_list_item_1, frequency));
                    listView.setOnItemClickListener(FrequenctActivity.this);
                }
            }

            @Override
            public void onFailure(Call<FrequenctBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (dataBeanList == null) {
            return;
        }
        webApi.sendFrequency(UserRepository.getInstance().getUserID(), getIntent().getIntExtra(KeyID, 0), dataBeanList.get(i).getId()).enqueue(new Callback<CommonBean>() {
            @Override
            public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                if(response.body()!=null) {
                    if (response.body().isResult()) {
                        showToast("举报成功");
                        finish();
                    } else {
                        showToast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<CommonBean> call, Throwable t) {

            }
        });
    }
}
